package com.example.petstoreservice.controller;

import com.example.petstoreservice.entity.Review;
import com.example.petstoreservice.entity.Result;
import com.example.petstoreservice.service.ReviewService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;

    @GetMapping("/all")
    public Result<List<Review>> getAllReviews() {
        Result<List<Review>> result = new Result<>();
        result.setData(reviewService.getAllReviews());
        return result;
    }

    @GetMapping("/product/{productId}")
    public Result<List<Review>> getReviewsByProductId(@PathVariable int productId) {
        Result<List<Review>> result = new Result<>();
        result.setData(reviewService.getReviewsByProductId(productId));
        return result;
    }

    @PostMapping("/add")
    public Result<?> insertReview(@RequestBody Review review) {
        if (reviewService.insertReview(review)) {
            return Result.success("添加评价成功");
        } else {
            return Result.fail("添加评价失败");
        }
    }

    @PostMapping("/update")
    public Result<?> updateReview(@RequestBody Review review) {
        if (reviewService.updateReview(review)) {
            return Result.success("更新评价成功");
        } else {
            return Result.fail("更新评价失败");
        }
    }

    @DeleteMapping("/delete/{reviewId}")
    public Result<?> deleteReview(@PathVariable int reviewId) {
        if (reviewService.deleteReview(reviewId)) {
            return Result.success("删除评价成功");
        } else {
            return Result.fail("删除评价失败");
        }
    }
    @GetMapping("/user/{username}")
    public Result<List<Review>> getReviewsByUsername(@PathVariable String username) {
        Result<List<Review>> result = new Result<>();
        result.setData(reviewService.getReviewsByUsername(username));
        return result;
    }
}
